package com.pro_one.controller;

import com.pro_one.constant.MessageConstant;
import com.pro_one.entity.PageResult;
import com.pro_one.entity.QueryPageBean;
import com.pro_one.entity.Result;
import com.pro_one.pojo.Exercise;
import com.pro_one.service.ExerciseService;
import com.pro_one.util.POIUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("exercise")
public class ExerciseController {
    @Reference
   private ExerciseService exerciseService;
    /*文件上传*/
    @RequestMapping("upload")
    private void upload(@RequestParam("excelFile") MultipartFile excelFile){


        try {
            List<String[]> excelFiles = POIUtils.readExcel(excelFile);
            if (excelFiles!=null&&excelFiles.size()>0) {
                List<Exercise> list = new ArrayList<>();
                for (String[] file : excelFiles) {
                        String number = file[0];
                        String code = file[1];
                        String name = file[2];
                        String runduration = file[3];
                        String frequency = file[4];
                        String intensity = file[5];//int型
                        String minage = file[6];//int型
                        String maxage = file[7];//int型
                        String part = file[8];
                        String motion = file[9];
                        String kcal = file[10];
                        String kj = file[11];

                        list.add(new Exercise(number, code, name, runduration, frequency, intensity, minage,
                                maxage, part, motion, kcal, kj));
                    }

                exerciseService.upload(list);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /*分页查询*/
    @RequestMapping("findByCondition")
    public PageResult findByCondition(@RequestBody QueryPageBean queryPageBean){

        return exerciseService.findByCondition(queryPageBean);
    }
    /*增加*/
    @RequestMapping("add")
    public Result add(@RequestBody Exercise exercise){
        try {
            exerciseService.add(exercise);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_EXERCUSE_FAIL);
        }
        return new Result(true,MessageConstant.ADD_EXERCUSE_SUCCESS);
    }
}
